package org.idea.spring.framework.event;

import org.springframework.context.*;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 事件发布器
 *
 * @Author linhao
 * @Date created in 4:47 下午 2021/5/23
 */
public class ApplicationEventPublisherDemo implements ApplicationEventPublisherAware {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(ApplicationEventPublisherDemo.class);
        annotationConfigApplicationContext.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
            @Override
            public void onApplicationEvent(ApplicationEvent event) {
                System.out.println("application listener 接收到消息：" + event);
            }
        });
        annotationConfigApplicationContext.addApplicationListener(new ApplicationListener<PayloadApplicationEvent>() {
            @Override
            public void onApplicationEvent(PayloadApplicationEvent event) {
                System.out.println("application listener 接收到消息：" + event);
            }
        });
        annotationConfigApplicationContext.refresh();
        annotationConfigApplicationContext.close();
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        System.out.println("====");
        //pay load事件
        applicationEventPublisher.publishEvent(new MyEvent("hello world") {
        });
        applicationEventPublisher.publishEvent("pay load event");
    }
}
